package cn.iocoder.yudao.module.design.controller.admin.user;

import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.design.controller.admin.user.vo.DesignUserUpdateInfoReqVO;
import cn.iocoder.yudao.module.design.service.user.DesignUserService;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import cn.iocoder.yudao.module.system.service.social.SocialClientService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.xingyuv.jushauth.model.AuthUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/design/user")
@Validated
public class DesignUserController {

    @Resource
    private DesignUserService userService;

    @Resource
    private SocialClientService socialClientService;


    @PostMapping("updateUserInfo")
    @Operation(summary = "修改用户")
    @PreAuthorize("@ss.hasPermission('system:user:update')")
    public CommonResult<Boolean> updateUserInfo(@Valid @RequestBody DesignUserUpdateInfoReqVO reqVO) {
        userService.updateUserInfo(reqVO.getId(), reqVO.getNickname(), reqVO.getAvatar());
        return success(true);
    }

    // 获取openId
    @GetMapping("/getOpenId")
    @Operation(summary = "获取openId")
    @PreAuthorize("@ss.hasPermission('system:user:update')")
    public CommonResult<String> getOpenId(@RequestParam("code") String code) {
        // 1. 先查找默认的配置项，从 application-*.yaml 中读取
//        AuthRequest request = yudaoAuthRequestFactory.get(SocialTypeEnum.valueOfType(SocialTypeEnum.WECHAT_MINI_APP.getType()).getSource());
//        AuthToken.builder().var
//        AuthResponse authResponse = request.userInfo(code);
        AuthUser authUser = socialClientService.getAuthUser(SocialTypeEnum.WECHAT_MINI_APP.getType(), 3, code, "yiju");
        String uuid = authUser.getUuid();
        return success(uuid);
    }


}
